﻿using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JaxaraRnD.Helpers.DataAccess
{
    public class FluentNHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)

                    InitializeSessionFactory();
                return _sessionFactory;
            }
        }

        private static void InitializeSessionFactory()
        {
            _sessionFactory = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(JaxaraRnDUtility.Constants.DEFAULT_CONNECTION).ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<FluentNHibernateHelper>())
                .ExposeConfiguration(c => new SchemaExport(c).Create(true, true))
                .BuildSessionFactory();
        }      

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}